package pattern.composite

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

/**
 * 分支节点
 */
class Composite(override protected val name:String) extends Component{

  private val children = new ArrayBuffer[Component]()

  override def add(component: Component): Unit ={
    children += component
  }

  override def remove(component: Component): Unit ={
    val i = children.indexOf(component)
    if(i>0) children.remove(i)
  }

  override def display(level: Int): Unit = {
    println("-"*level+name)
    for(component<-children) component.display(level+2)
  }
}
